/*
 * Copyright (c) 2015. For Intelligent Group.
 */
package com.intelligent.ispc.client.core.modal.grr;

import com.google.common.collect.Lists;
import com.intelligent.ispc.constant.AppConstant;
import com.intelligent.ispc.core.dto.GrrGroupDto;

import javax.swing.*;
import java.util.List;
import java.util.Vector;
/**
 * Created by cherry on 16/9/10.
 */

/**
 * @param <T>
 */
public class GroupModel<T> extends DefaultComboBoxModel<T> {

    private Vector<T> comboBoxValues;
    private Object selectedValue;
    private GrrGroupDto grrGroup;
    private GrrGroupDto selectedGroup;

    /**
     * GroupModel constructor
     */
    public GroupModel() {
        comboBoxValues = new Vector<>();
    }


    @Override
    public int getSize() {

        if (comboBoxValues == null) {
            return 0;
        }
        return comboBoxValues.size();
    }

    @Override
    public T getElementAt(int index) {
        if (index >= 0 && index < comboBoxValues.size()) {
            return comboBoxValues.elementAt(index);
        } else {
            return null;
        }
    }

    @Override
    public void setSelectedItem(Object anObject) {

        if ((selectedValue != null && !selectedValue.equals(anObject)) || selectedGroup == null && anObject != null) {
            selectedValue = anObject;
            fireContentsChanged(this, -1, -1);
        }
    }

    @Override
    public Object getSelectedItem() {
        return selectedValue;
    }

    @Override
    public void removeAllElements() {

        grrGroup = null;
        selectedGroup = null;

        if (comboBoxValues != null && comboBoxValues.size() > 0) {
            int firstIndex = 0;
            int lastIndex = comboBoxValues.size() - 1;
            comboBoxValues.removeAllElements();
            selectedValue = null;
            fireIntervalRemoved(this, firstIndex, lastIndex);

        } else {
            selectedValue = null;
        }
    }

    /**
     * set groupModel data source
     *
     * @param grrGroup groupModel data source
     */
    public void setData(GrrGroupDto grrGroup) {

        this.grrGroup = grrGroup;
        selectedGroup = new GrrGroupDto();
        List<String> oldGroups = null;
        List<String> newGroup = null;

        if (grrGroup != null) {
            oldGroups = grrGroup.getValue();
            if (oldGroups != null && !oldGroups.isEmpty()) {
                newGroup = replaceStr(oldGroups, AppConstant.GLOBAL_SEPARATOR, "_");
            }
        }

        if (newGroup == null) {
            newGroup = Lists.newArrayList();
        }

        comboBoxValues = new Vector(newGroup);
        if (getSize() > 0) {
            selectedValue = getElementAt(0);
        }
        fireContentsChanged(this, 0, newGroup.size());
    }

    private List<String> replaceStr(List<String> oldStr, String oldSplit, String newSplit) {

        List<String> newStr = Lists.newArrayList();
        for (String str : oldStr) {
            newStr.add(str.replace(oldSplit, newSplit));
        }
        return newStr;
    }

    /**
     * package group object by selected index
     *
     * @param index current selected index
     */
    public void setSelectedGroup(int index) {

        if (index == -1 || grrGroup == null || selectedGroup == null) {
            return;
        }
        String selectedGroupValue = grrGroup.getValue().get(index);
        List<String> selectGroupValues = Lists.newArrayList();
        selectGroupValues.add(selectedGroupValue);

        selectedGroup.setGroupkey1(grrGroup.getGroupkey1());
        selectedGroup.setGroupValue1(grrGroup.getGroupValue1());
        selectedGroup.setGroupkey2(grrGroup.getGroupkey2());
        selectedGroup.setGroupValue2(grrGroup.getGroupValue2());
        selectedGroup.setDimensionValue(grrGroup.getDimensionValue());

        selectedGroup.setValue(selectGroupValues);
    }

    public GrrGroupDto getSelectedGroup() {
        return selectedGroup;
    }
}
